package com.ruoyi.common.utils;

import java.text.DecimalFormat;

public class DriverUtils {

    public static String  surName(String name){
        if(StringUtils.isEmpty(name)){
            return null;
        }
        if(name.length()<2){
            return name;
        }
        return name.substring(0,1);
    }
    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static String getDistance(String lat1, String lng1, String lat2,
                                     String lng2) {
        try{
            double radLat1 = rad(Double.valueOf(lat1));
            double radLat2 = rad(Double.valueOf(lat2));
            double a = radLat1 - radLat2;
            double b = rad(Double.valueOf(lng1)) - rad(Double.valueOf(lng2));
            double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                    + Math.cos(radLat1) * Math.cos(radLat2)
                    * Math.pow(Math.sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.round(s * 10000d) / 10000d;
            DecimalFormat df = new DecimalFormat("#.00");
            return df.format(s);
        }catch (Exception e){
           e.printStackTrace();
        }
       return "0";
    }

    public static void main(String[] args) {
        System.out.println(getDistance("113.69347863480448","34.78346029149259","113.69347863480448","32.78346029149259"));
    }
}
